import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router'
import useStore from '@/store'

const routes: Array<RouteRecordRaw> = [
  {
    path: '/',
    name: 'home',
    component: () => import('@/layout/index.vue'),
    redirect: '/shop',
    children: [
      {
        path: 'shop',
        name: 'shop',
        meta: {
          showToolBar: true,
          showTopBar: true,
          showHeader: true
        },
        component: () => import('@/views/shop/index.vue')
      },
      {
        path: 'search',
        name: 'search',
        meta: {
          showToolBar: false,
          showHeader: true,
          showTopBar: true
        },
        component: () => import('@/views/search/index.vue')
      },
      {
        path: 'cart',
        name: 'cart',
        meta: {
          showToolBar: false,
          showTopBar: false,
          showHeader: false
        },
        component: () => import('@/views/cart/index.vue')
      },
      {
        path: 'order',
        name: 'order',
        meta: {
          showToolBar: false,
          showTopBar: false,
          showHeader: false
        },
        component: () => import('@/views/order/index.vue')
      },
      {
        path: 'detail',
        name: 'detail',
        meta: {
          showToolBar: true,
          showTopBar: true,
          showHeader: true
        },
        component: () => import('@/views/detail/index.vue')
      },
      {
        path: 'success',
        name: 'success',
        meta: {
          showToolBar: true,
          showTopBar: true,
          showHeader: true
        },
        component: () => import('@/views/success/index.vue')
      },
      {
        path: 'confirm',
        name: 'confirm',
        meta: {
          showToolBar: false,
          showTopBar: false,
          showHeader: false
        },
        component: () => import('@/views/confirm/index.vue')
      },
      {
        path: 'user',
        name: 'user',
        meta: {
          showToolBar: true,
          showTopBar: true,
          showHeader: true
        },
        component: () => import('@/views/user/index.vue'),
        redirect: '/user/portal',
        children: [
          {
            path: 'portal',
            name: 'portal',
            component: () => import('@/views/user/portal/index.vue')
          },
          {
            path: 'orderList',
            name: 'orderList',
            component: () => import('@/views/user/orderList/index.vue')
          },
          {
            path: 'address',
            name: 'address',
            component: () => import('@/views/user/address/index.vue')
          },
          {
            path: 'account',
            name: 'account',
            component: () => import('@/views/user/account/index.vue')
          },
          {
            path: 'message',
            name: 'message',
            component: () => import('@/views/user/message/index.vue')
          }
        ]
      }
    ]
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('@/views/loginRegister/index.vue')
  },
  {
    path: '/register',
    name: 'register',
    component: () => import('@/views/loginRegister/index.vue')
  }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})
//全局守卫：前置守卫（在路由跳转之间进行判断）
router.beforeEach(async (to, from, next) => {
  document.documentElement.scrollTop = 0
  const { appStore, userStore } = useStore()
  const token = appStore.token
  const nickName = userStore.nickName
  // 用户登录了
  if (token) {
    //用户登录了,不能去login
    if (to.path == '/login' || to.path == '/register') {
      next('/shop')
    } else {
      //用户登陆了,而且还有用户信息【去的并非是login】
      if (nickName) {
        next()
      } else {
        //用户登陆了,但是没有用户信息
        try {
          //发请求获取用户信息以后在放行
          await userStore.getUserInfo()
          next()
        } catch (error) {
          //用户没有信息，还携带token发请求获取用户信息【失败】
          //token【学生证失效了】
          //token失效:本地清空数据、服务器的token通知服务器清除
          await appStore.logout()
          //回到登录页，重新获取一个新的学生证
          next('/login')
        }
      }
    }
  } else {
    //用户未登录||目前的判断都是放行.将来这里会'回手掏'增加一些判断
    //用户未登录:不能进入/trade、/pay、/paysuccess、/center、/center/myorder  /center/teamorder
    const toPath = to.path
    if (
      toPath.indexOf('order') != -1 ||
      toPath.indexOf('pay') != -1 ||
      toPath.indexOf('center') != -1
    ) {
      next('/login')
    } else {
      next()
    }
  }
})
export default router
